package com.dong.rabbitmq.consumer;

import com.dong.rabbitmq.config.DelayedQueueConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author dongjian
 *
 * rabbitmq-delayed-message-exchanger 延迟队列消费者
 */
@Slf4j
@Component
public class DelayedQueueConsumer {

    @RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)
    public void receiveDelayMsg(Message message, Channel channel) throws UnsupportedEncodingException {
        String  msg = new String(message.getBody(), "UTF-8");
        log.info("当前时间 {}, 收到一条来自delayed.queue队列的消息：{}", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), msg);
    }
}
